package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysNovelAuthorMapper;
import com.ruoyi.system.mapper.SysNovelClassifyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysNovelBookMapper;
import com.ruoyi.system.domain.SysNovelBook;
import com.ruoyi.system.service.ISysNovelBookService;

/**
 * 书籍列表Service业务层处理
 *
 * @author ZouYangMing
 * @date 2022-11-08
 */
@Service
public class SysNovelBookServiceImpl implements ISysNovelBookService
{
    @Autowired
    private SysNovelBookMapper sysNovelBookMapper;

    @Autowired
    private SysNovelAuthorMapper sysNovelAuthorMapper;

    @Autowired
    private SysNovelClassifyMapper sysNovelClassifyMapper;

    @Autowired
    private RedisCache redisCache;

    private final String SYS_NOVEL_BOOK = "sys_novel_book:";

    /**
     * 查询书籍信息
     *
     * @param id 书籍主键
     * @return 书籍
     */
    @Override
    public SysNovelBook selectSysNovelBookById(Long id)
    {
        SysNovelBook cacheSet = redisCache.getCacheObject(SYS_NOVEL_BOOK + id);
        if (cacheSet == null) {
            //如果不存在则执行
            SysNovelBook book = sysNovelBookMapper.selectSysNovelBookById(id);
            redisCache.setCacheObject(SYS_NOVEL_BOOK + id, book);
            return book;
        }
        return cacheSet;
    }

    /**
     * 查询书籍列表
     *
     * @param sysNovelBook 书籍信息
     * @return 书籍列表
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookList(SysNovelBook sysNovelBook)
    {
        List<SysNovelBook> list = sysNovelBookMapper.selectSysNovelBookList(sysNovelBook);
        for (SysNovelBook novelBook : list) {
            if (novelBook.getClassifyId() != null) {
                novelBook.setClassifyName(sysNovelClassifyMapper.selectSysNovelClassifyById(novelBook.getClassifyId()).getClassifyName());
            }
            if(novelBook.getAuthorId() != null){
                novelBook.setAuthorName(sysNovelAuthorMapper.selectSysNovelAuthorById(novelBook.getAuthorId()).getAuthorStageName());
            }
        }
        return list;
    }

    /**
     * 新增书籍列表
     *
     * @param sysNovelBook 书籍列表
     * @return 结果
     */
    @Override
    public int insertSysNovelBook(SysNovelBook sysNovelBook)
    {
        sysNovelBook.setCreateBy(SecurityUtils.getUsername());
        sysNovelBook.setCreateTime(DateUtils.getNowDate());
        int index = sysNovelBookMapper.insertSysNovelBook(sysNovelBook);
        redisCache.setCacheObject(SYS_NOVEL_BOOK + sysNovelBook.getId(), sysNovelBook);
        return index;
    }

    /**
     * 修改书籍列表
     *
     * @param sysNovelBook 书籍列表
     * @return 结果
     */
    @Override
    public int updateSysNovelBook(SysNovelBook sysNovelBook)
    {
        sysNovelBook.setUpdateBy(SecurityUtils.getUsername());
        sysNovelBook.setUpdateTime(DateUtils.getNowDate());
        if(sysNovelBook.getBookIsPay().equals("1")){
            sysNovelBook.setBookPayMoney(new BigDecimal(0));
        }
        int index = sysNovelBookMapper.updateSysNovelBook(sysNovelBook);
        redisCache.deleteObject(SYS_NOVEL_BOOK + sysNovelBook.getId());
        redisCache.setCacheObject(SYS_NOVEL_BOOK + sysNovelBook.getId(), sysNovelBook);
        return index;
    }

    /**
     * 批量删除书籍列表
     *
     * @param ids 需要删除的书籍列表主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelBookByIds(Long[] ids)
    {
        for (Long id : ids) {
            redisCache.deleteObject(SYS_NOVEL_BOOK + id);
        }
        return sysNovelBookMapper.deleteSysNovelBookByIds(ids);
    }

    /**
     * 删除书籍列表信息
     *
     * @param id 书籍列表主键
     * @return 结果
     */
    @Override
    public int deleteSysNovelBookById(Long id)
    {
        redisCache.deleteObject(SYS_NOVEL_BOOK + id);
        return sysNovelBookMapper.deleteSysNovelBookById(id);
    }

    /**
     * 查询书籍按出版时间排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookFontNumber() {
        return sysNovelBookMapper.selectSysNovelBookListBookFontNumber();
    }

    /**
     * 查询书籍按出版时间排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookStartTime() {
        return sysNovelBookMapper.selectSysNovelBookListBookStartTime();
    }

    /**
     * 查询书籍按购买数量排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookPayNumber() {
        return sysNovelBookMapper.selectSysNovelBookListBookPayNumber();
    }

    /**
     * 查询书籍按完结时间排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookEndTime() {
        return sysNovelBookMapper.selectSysNovelBookListBookEndTime();
    }

    /**
     * 查询书籍按浏览量排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookBrowseNumber() {
        return sysNovelBookMapper.selectSysNovelBookListBookBrowseNumber();
    }

    /**
     * 查询书籍按收藏量排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookCollectionNumber() {
        return sysNovelBookMapper.selectSysNovelBookListBookCollectionNumber();
    }

    /**
     * 查询书籍按更新时间排序
     *
     * @return 书籍列表集合
     */
    @Override
    public List<SysNovelBook> selectSysNovelBookListBookUpdateTime() {
        return sysNovelBookMapper.selectSysNovelBookListBookUpdateTime();
    }
}
